import apis from "../server/App&BusinessManagementApi";

const {
  appDetail,
  appDelete,
  busiDelete,
  appEdit,
  tenantList,
  addApp,
  addBusi,
  busiUpdate,
  authorizedUsersList,
  authorizeUser,
  authorizeCancel,
  listTenantUsers,
} = apis;

const initData = {
  appInfo: {},
  tenantList: [],
  appList: [],
  authorizedUsersList: [],
  tenantUsersList: [],
  usersTotalCount: 0,
};

export default {
  namespace: "AppBusinessManagement",
  state: {
    ...initData,
  },

  effects: {
    *getAppDetail({ payload }, { call, put }) {
      const { errCode, errMsg, datas = [] } = yield call(appDetail, payload);
      yield put({ type: "get/AppDetail", payload: datas });
      return { errCode, errMsg, datas };
    },
    *AppDelete({ payload }, { call }) {
      const { errCode, errMsg } = yield call(appDelete, payload);
      return { errCode, errMsg };
    },
    *BusiDelete({ payload }, { call }) {
      const { errCode, errMsg, errortext } = yield call(busiDelete, payload);
      return { errCode, errMsg: errMsg || errortext };
    },
    *AppEdit({ payload }, { call }) {
      const { errCode, errMsg } = yield call(appEdit, payload);
      return { errCode, errMsg };
    },
    *BusiUpdate({ payload }, { call }) {
      const { errCode, errMsg, errortext } = yield call(busiUpdate, payload);
      return { errCode, errMsg: errMsg || errortext };
    },
    *getTenantList({ payload }, { call, put }) {
      const { errCode, errMsg, datas = [] } = yield call(tenantList, payload);
      yield put({ type: "get/TenantList", payload: datas });
      return { errCode, errMsg, datas };
    },
    *addApp({ payload }, { call }) {
      const { errCode, errortext: errMsg } = yield call(addApp, payload);
      return { errCode, errMsg };
    },
    *addBusi({ payload }, { call }) {
      const { errCode, errortext: errMsg } = yield call(addBusi, payload);
      return { errCode, errMsg };
    },
    *getAuthorizedUsersList({ payload }, { call, put }) {
      const { errCode, errMsg, datas = [], totalCount = 0 } = yield call(
        authorizedUsersList,
        payload,
      );
      yield put({
        type: "get/AuthorizedUsersList",
        payload: { datas, totalCount },
      });
      return { errCode, errMsg, datas, totalCount };
    },
    *authorizeUser({ payload }, { call }) {
      const { errCode, errMsg } = yield call(authorizeUser, payload);
      return { errCode, errMsg };
    },
    *authorizeCancel({ payload }, { call }) {
      const { errCode, errMsg } = yield call(authorizeCancel, payload);
      return { errCode, errMsg };
    },
    *listTenantUsers({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas } = yield call(
        listTenantUsers,
        payload,
      );
      yield put({
        type: "get/TenantUsersList",
        payload: { datas },
      });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
  },
  reducers: {
    "get/AppDetail": (state, { payload }) => ({
      ...state,
      appInfo: payload,
    }),
    "get/TenantList": (state, { payload }) => ({
      ...state,
      tenantList: payload,
    }),
    "get/AuthorizedUsersList": (state, { payload }) => ({
      ...state,
      authorizedUsersList: payload.datas,
      usersTotalCount: payload.totalCount,
    }),
    "get/TenantUsersList": (state, { payload }) => ({
      ...state,
      tenantUsersList: payload.datas,
    }),
    clearData: () => ({ ...initData }),
  },
};
